package cn.slipi.admin.service.impl;

import cn.slipi.admin.common.exception.base.BizException;
import cn.slipi.admin.common.utils.StrUtils;
import cn.slipi.admin.common.utils.dozer.impl.ConvertBean;
import cn.slipi.admin.entity.PdrProject;
import cn.slipi.admin.dao.PdrProjectMapper;
import cn.slipi.admin.service.PdrProjectService;
import cn.slipi.admin.service.dto.in.producer.ListDTO;
import cn.slipi.admin.service.dto.in.producer.SaveDTO;
import cn.slipi.admin.service.dto.out.KeyUpdateInfo;
import cn.slipi.admin.service.dto.out.ProjectUpdateDTO;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 生产者项目（服务提供者） 服务实现类
 * </p>
 *
 * @author lee
 * @since 2020-12-01
 */
@Service
public class PdrProjectServiceImpl extends ServiceImpl<PdrProjectMapper, PdrProject> implements PdrProjectService {

    @Autowired
    private ConvertBean convertBean;

    @Override
    public List<PdrProject> getDropList() {
        QueryWrapper<PdrProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = BizException.class)
    public IPage<PdrProject> getList(ListDTO dto) {
        IPage<PdrProject> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        QueryWrapper<PdrProject> queryWrapper = new QueryWrapper<>();

        if (dto.getEnable() != null) {
            queryWrapper.eq("enable", dto.getEnable());
        }
        if (StrUtils.isNotBlank(dto.getName())) {
            queryWrapper.like("name", dto.getName());
        }
        if (StrUtils.isNotBlank(dto.getDescription())) {
            queryWrapper.like("description", dto.getDescription());
        }

        queryWrapper.eq("status", 1);

        return this.baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = BizException.class)
    public PdrProject add(SaveDTO saveDTO) {
        PdrProject project = this.getByName(null, saveDTO.getName());
        if (project != null) {
            throw new BizException(10000, "record.key.found");
        }
        project = this.convertBean.convert(saveDTO, PdrProject.class);
        project.setHystrixParam("{}");
        project.setGrayRelease(JSONObject.toJSONString(saveDTO.getGrayRelease()));
        project.setLoadBalance(JSONObject.toJSONString(saveDTO.getLoadBalance()));
        this.baseMapper.insert(project);
        return project;
    }

    @Override
    @Transactional(rollbackFor = BizException.class)
    public ProjectUpdateDTO update(Integer id, SaveDTO saveDTO) {
        ProjectUpdateDTO projectUpdateDTO = new ProjectUpdateDTO();
        PdrProject project = this.getById(id);
        if (project == null) {
            throw new BizException(10000, "record.found.not");
        }
        KeyUpdateInfo keyUpdateInfo = isUpdateKey(saveDTO, project);
        PdrProject pdrName = this.getByName(id, saveDTO.getName());
        if (pdrName != null) {
            throw new BizException(10000, "record.key.found");
        }
        this.convertBean.convertNotNull(saveDTO, project, false, true);
//        project.setHystrixParam(JSONObject.toJSONString(saveDTO.getHystrixParam()));
        project.setGrayRelease(JSONObject.toJSONString(saveDTO.getGrayRelease()));
        project.setLoadBalance(JSONObject.toJSONString(saveDTO.getLoadBalance()));
        this.baseMapper.updateById(project);
        //是否更新关连接口
        if (keyUpdateInfo == null) {
            projectUpdateDTO.setKeyUpdate(false);
        } else {
            projectUpdateDTO.setKeyUpdate(true);
            projectUpdateDTO.setKeyUpdateInfo(keyUpdateInfo);
        }
        projectUpdateDTO.setPdrProject(project);
        return projectUpdateDTO;
    }

    private KeyUpdateInfo isUpdateKey(SaveDTO saveDTO, PdrProject project) {
        KeyUpdateInfo keyUpdateInfo = null;
        String grayRelease = JSONObject.toJSONString(saveDTO.getGrayRelease());
        String loadBalance = JSONObject.toJSONString(saveDTO.getLoadBalance());
        //如果关闭项目则关闭接口
        boolean unEnable = saveDTO.getEnable().equals(0);
        boolean upLoadIp = !project.getLoadIp().equalsIgnoreCase(saveDTO.getLoadIp());
        boolean upGrayRelease = !project.getGrayRelease().equalsIgnoreCase(grayRelease);
        boolean upLoadBalance = !project.getLoadBalance().equalsIgnoreCase(loadBalance);
        boolean upProtocol = !project.getProtocol().equalsIgnoreCase(saveDTO.getProtocol());
        boolean upTranslateProtocol = !project.getTranslateProtocol().equalsIgnoreCase(saveDTO.getTranslateProtocol());
        if (unEnable || upLoadIp || upProtocol || upGrayRelease || upLoadBalance || upTranslateProtocol) {
            keyUpdateInfo = new KeyUpdateInfo();
        }
        if (unEnable) {
            keyUpdateInfo.setEnable(saveDTO.getEnable());
        }
        if (upLoadIp) {
            keyUpdateInfo.setLoadIp(saveDTO.getLoadIp());
        }
        if (upProtocol) {
            keyUpdateInfo.setProtocol(saveDTO.getProtocol());
        }
        if (upGrayRelease) {
            keyUpdateInfo.setGrayRelease(grayRelease);
        }
        if (upLoadBalance) {
            keyUpdateInfo.setLoadBalance(loadBalance);
        }
        if (upTranslateProtocol) {
            keyUpdateInfo.setTranslateProtocol(saveDTO.getTranslateProtocol());
        }
        return keyUpdateInfo;
    }

    @Override
    public PdrProject detail(Integer id) {
        PdrProject project = this.getById(id);
        if (project == null) {
            throw new BizException(10000, "record.found.not");
        }
        return project;
    }

    @Override
    public List<PdrProject> getPdrListByIds(List<Integer> ids) {
        QueryWrapper<PdrProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        queryWrapper.eq("status", 1);
        return this.baseMapper.selectList(queryWrapper);
    }

    private PdrProject getById(Integer id) {
        QueryWrapper<PdrProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("status", 1);
        return this.baseMapper.selectOne(queryWrapper);
    }

    private PdrProject getByName(Integer id, String name) {
        QueryWrapper<PdrProject> queryWrapper = new QueryWrapper<>();
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        queryWrapper.eq("name", name);
        queryWrapper.eq("status", 1);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
